import Vue from "vue"
import VueRouter from "vue-router"

Vue.use(VueRouter)

const initRouter = () => {
  const requireRoutes = require.context(
    "./routes",
    true,
    /\.js$/
  )
  const routes = requireRoutes.keys().map((item) => {
    return requireRoutes(item)
  })
  return routes
}

const routes = [
  {
    path: "/",
    name: "/",
    component: () => import("@/views/layout"),
    redirect: "/home",
    children: [...initRouter()],
  },
]
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function(location) {
  return originalPush
    .call(this, location)
    .catch((err) => err)
}

const router = new VueRouter({
  routes,
})
router.beforeEach((to, from, next) => {
  // 路由拦截
  // if (to.path !== "/Login") {
  //   if (sessionStorage.getItem("Authorization")) {
  //     next()
  //   } else {
  //     next("/Login")
  //   }
  // } else {
  next()
  // }
})
export default router
